<extend name="Layout/ins_page" />
<block name="content">
	{:W('PageHeader/simple',array('name'=>'半成品出库','search'=>'N'))}
	<form method='post' id="form_data" name="form_data" enctype="multipart/form-data"   class="well form-horizontal">
		<input type="hidden" id="type" name="type" value="2" />
		<div class="form-group col-sm-6">
			<label class="col-sm-4 control-label">出库半成品：</label>
			<div class="col-sm-8">
				<select id="storehouse_product_id" name="storehouse_product_id" class="form-control" check="require"  msg="请输入出库产品" onchange="show_number(this.options[this.options.selectedIndex].value)">
					<option value="">请选择</option>
					<foreach name="product" item="vo">
						<option value="{$vo.id}">{$vo.product}|{$vo.product_bh}</option>
					</foreach>
				</select>
			</div>
		</div>
		
		<div class="form-group col-sm-6 nownumber">
			<label class="col-sm-4 control-label">现有库存：</label>
			<div class="col-sm-8">
				<input class="form-control" id="now-number" type="text" name="now_number" disabled  value="0"/>
			</div>
		</div>
		
		<div class="form-group col-sm-6 convert-unit hide">
			<label class="col-sm-4 control-label">转换单位：</label>
			<div class="col-sm-8">
				<input class="form-control" id="convert_unit" type="text" name="convert_unit" disabled  value=""/>
			</div>
		</div>
		
		<div class="form-group col-sm-6 convert-number hide">
			<label class="col-sm-4 control-label">转换数量：</label>
			<div class="col-sm-8">
				<input class="form-control" id="convert_number" type="text" name="convert_number" disabled  value=""/>
			</div>
		</div>
		
		<div class="form-group col-sm-6 task-parent">
			<label class="col-sm-4 control-label">任务：</label>
			<div class="col-sm-8">
				<div class="task-chose">
					<select id="task-parent-id" name="task_parent_id[]" class="form-control" check="require"  msg="请选择任务" onchange="show_childtask(this.options[this.options.selectedIndex].value,this)">
						<option value="">请选择</option>
						<foreach name="taskParent" item="vo">
							<option value="{$vo.id}">{$vo.task_parent_name}</option>
						</foreach>
					</select>
					<select id="task-id" name="task_id[]" class="form-control" check="require"  msg="请选择流程" onchange="show_user(this.options[this.options.selectedIndex].value,this)">
						<option value="">请选择</option>
					</select>
					<select id="task-user-id" name="task_user_id[]" class="form-control" check="require"  msg="请选择接收人">
						<option value="">请选择</option>
					</select>
				</div>
				<a class="btn btn-sm btn-primary task-add">添加</a>
			</div>
		</div>
		<!-- 
		<div class="form-group col-sm-6 task-child">
			<label class="col-sm-4 control-label">流程：</label>
			<div class="col-sm-8">
				<select id="task-id" name="task_id" class="form-control" check="require"  msg="请选择流程" onchange="show_user(this.options[this.options.selectedIndex].value)">
					<option value="">请选择</option>
					
				</select>
			</div>
		</div>
		
		<div class="form-group col-sm-6 task-user">
			<label class="col-sm-4 control-label">接收人：</label>
			<div class="col-sm-8">
				<select id="task-user-id" name="task_user_id" class="form-control" check="require"  msg="请选择接收人">
					<option value="">请选择</option>
				</select>
			</div>
		</div>
		 -->
		<div class="form-group col-sm-6">
			<label class="col-sm-4 control-label" >数量：</label>
			<div class="col-sm-8">
				<input class="form-control" id="number" type="text" name="number" check="require"  msg="请输入出库产品数量"/>
			</div>
		</div>

		<div class="form-group col-sm-6">
			<label class="col-sm-4 control-label" for="opreate_name">经办人：</label>
			<div class="col-sm-8">
				<input class="form-control" readonly type="text" id="opreate_name" name="opreate_name" check="require" msg="请输入经办人" value="{$opreate_name}">
			</div>
		</div>
		<div class="form-group col-sm-6">
			<label class="col-sm-4 control-label" for="name">摘要：</label>
			<div class="col-sm-8">
				<input class="form-control" type="text" id="remark" name="remark"  />
			</div>
		</div>
		<div class="form-group col-sm-6">
			<label class="col-sm-4 control-label" for="name">图片：</label>
			<div class="col-sm-8">
				<input  type="file" id="product_img" name="product_img" />
			</div>
		</div>
		<div class="form-group">
			<div class="form-actions col-sm-10 col-sm-offset-2">
				<input class="btn btn-sm btn-primary" type="button" value="保存" onclick="save();">
				<input class="btn btn-sm btn-default" type="button" value="取消" onclick="location.href='{:U('index')}'" />
			</div>
		</div>
	</form>
</block>
<block name="js">
	<script>
		
		function save() {
			var size = $('.task-parent').size();
			var now_number = parseInt($('#now-number').val());
			var number = parseInt($('#number').val());
			if (number*size > now_number) {
				ui_error('出库数量:'+(number*size)+'大于现有库存数量');
				return ;
			}
			sendForm("form_data", "{:U('addHalfPost')}");
		}
		
		function show_number(id) {
			if(id.length<=0) {
				$('#now-number').val(0);
				return ;
			}
			var post_url = "{:U('showNum')}";
			$.ajax({
				type : "POST",
				dataType : "json",
				data : 'id='+id,
				url : post_url,
				success : function(data) {
					if (data.status) {
						$('.nownumber').removeClass('hide');
						$('#now-number').val(data.info.num);
						if( (data.info.convert_number > 0) && data.info.convert_unit !='') {
							$('.convert-unit').removeClass('hide');
							$('.convert-number').removeClass('hide');
							$('#convert_number').val(data.info.convert_number);
							$('#convert_unit').val(data.info.convert_unit);
						} else {
							$('.convert-unit').addClass('hide');
							$('.convert-number').addClass('hide');
							$('#convert_number').val(data.info.convert_number);
							$('#convert_unit').val(data.info.convert_unit);
						}
					} else {
						ui_error(data.info);
					}
				}
			})
		}
		
		/**
		 * 显示下级任务
		*/
		function show_childtask(id,obj) {
			
			var post_url = "{:U('show_childtask')}";
			var parent = $(obj).parents('.task-parent');
			parent.find('#task-id').find("option:not('option:first')").remove();
			parent.find('#task-user-id').find("option:not('option:first')").remove();
			$.ajax({
				type : "POST",
				dataType : "json",
				data : 'id='+id,
				url : post_url,
				success : function(data) {
					var opt = ''; 
					var con  = ''; 
					if (data.status) {
						$.each(data.info,function(key,value){
							con = '<option value="'+value.id+'">'+value.name+'</option>';
							opt += con;
						})
						parent.find('#task-id').append(opt);
					} else {
						ui_error(data.info);
					}
				}
			})
		}
		
		/**
		* 展现
		*/
		function show_user(id,obj) {
			
			var post_url = "{:U('show_user')}";
			var parent = $(obj).parents('.task-parent');
			parent.find('#task-user-id').find("option:not('option:first')").remove();
			$.ajax({
				type : "POST",
				dataType : "json",
				data : 'id='+id,
				url : post_url,
				success : function(data) {
					var opt = ''; 
					var con  = ''; 
					if (data.status) {
						$.each(data.info,function(key,value){
							con = '<option value="'+value+'">'+value.substring(0,value.lastIndexOf("|"))+'</option>';
							opt += con;
						})
						parent.find('#task-user-id').append(opt);
					} else {
						ui_error(data.info);
					}
				}
			})
		}
		$(function(){
			
			var copy = $(".task-chose:eq(0)").clone().html();
			//添加
			$('.task-parent').on('click','.task-add',function(){
				var html = '<div class="form-group col-sm-6 task-parent">' + 
							 	'<label class="col-sm-4 control-label">任务：</label>' +
							 	'<div class="col-sm-8">' +
							 		'<div class="task-chose">'+
							 			copy +
							 		'</div>'+
							 		'<a class="btn btn-sm btn-primary task-del">删除</a>' +
							      '</div>'+
							'</div>';
							 	
				$('.task-parent:last').after(html);		
			})
			
			$('#form_data').on('click','.task-del',function(){
				$(this).parents('.task-parent').remove();
			})
		})
		
	</script>
</block>